﻿using System;
using System.Net.Mail;
using System.Windows;
using System.Text;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// App.xaml 的交互逻辑
        /// </summary>
        public partial class App
        {
                public App()
                {
                        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                        Dispatcher.UnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);
                }

                

                void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
                {
                        MessageBox.Show("很抱歉程序发生了错误");
                }

                void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
                {
                        MessageBox.Show("很抱歉程序发生了错误，错误信息已发送至开发人员邮箱");

                        MailAddress mailAddress = new MailAddress("shuaihong617@qq.com");

                        MailMessage mailMessage = new MailMessage();
                        mailMessage.From = mailAddress;
                        mailMessage.To.Add(mailAddress);
                        mailMessage.Subject = "武汉大学出版社CRM 异常报告";

                        StringBuilder stringBuilder = new StringBuilder();

                        stringBuilder.AppendLine("异常:" + e.Exception.Message);
                        if (e.Exception.InnerException !=null)
                        {
                                stringBuilder.AppendLine("内部异常:" + e.Exception.InnerException.Message);
                        }
                        stringBuilder.AppendLine(e.Exception.StackTrace);
                        
                        mailMessage.Body = stringBuilder.ToString() ;

                        try
                        {
                                SmtpClient smtpClient = new SmtpClient("smtp.qq.com", 25);
                                smtpClient.EnableSsl = false;
                                smtpClient.UseDefaultCredentials = false;
                                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                                smtpClient.Credentials = new System.Net.NetworkCredential("shuaihong617", "wdbpwxby");

                                smtpClient.Send(mailMessage);  
                        }
                        catch (Exception)
                        {
                                MessageBox.Show("很抱歉错误信息发送失败，请联系系统管理员");
                        }

                        
                }
        }
}